#!/usr/bin/perl -w

#------------------------------------------------------
# This script is to extract Medlytix needed fields
# from the eScan data file, and print to STDOUT.
#
#	Usage:
#		escan_tran.pl    <datafile>
#	Author:
#		Tao Zhang	
#------------------------------------------------------ 


use strict;
use warnings;

#---------------------------------------------------
# globle variables
#---------------------------------------------------
our @hma_read_ins1 = qw(
  cust_id              
  hid                       
  acctnum           
  trans_id             
  trans_date      
  trans_amt        
  trans_type
  cdm       
  payor_plan              
  bill_date    
  denial_code
  denial_date
);

#---------------------------------------------------
# void print_record($)
#		print retrieved fields on the screen
#		Argument is the reference of a hash table
#		
#		no return;
#---------------------------------------------------
sub print_record($){
        my $hr = shift;

        foreach my $fld ( @hma_read_ins1 ) {
                my $x = $hr->{$fld};
                $x =~ s/^\s*//;
                $x =~ s/\s*$//;
                $x =~ s/\|//;
                print "$x";
                print "|" if $fld ne $hma_read_ins1[$#hma_read_ins1];
        }
        print "\n";
}

sub usage(){
	print STDERR "Usage:\n";
	print STDERR "\t$0 <datafile>\n";
}


#----------------------------------------------
# Processing input file or input file handle
#----------------------------------------------

#if(@ARGV < 1 ){
#	usage();
#	exit;
#}
#our $indata = $ARGV[0] || "-";
our $indata = "-";
our $INFD = undef;
our %dest;

if( $indata ne "-" ) 
{
	if ( ! -r $indata ) 
	{
		print STDERR " file $indata not accessable !!!\n";
		exit 1;
	}else 
	{
		open(HMA, "<$indata");
		$INFD = *HMA;
	}
} else 
{
	$INFD = *STDIN;
}


#-------------------------------------------------
# processing data record by record
#-------------------------------------------------

while(my $line = <$INFD> ) {
	chomp $line;
	$line =~ s/[^\x20-\x7e]//g;
	next if $line =~ /^\s*$/;
	my @fields = split( /\|/, $line);
	$dest{hid}     = $fields[0];
	$dest{acctnum} = $fields[1];
	$dest{trans_date} = $fields[3];
	$dest{trans_amt}   = $fields[4];
	$dest{trans_type}   = $fields[5];
        $dest{cdm}   = $fields[6];
	if ( $fields[7] eq "" )
	{
		$dest{payor_plan}   = $fields[6];
	}else
	{
		$dest{payor_plan}   = $fields[7];
	}
	$dest{bill_date}   = $fields[12];
	$dest{trans_id}     = $fields[13];
	$dest{cust_id}	= 49;   # HMA's id
        $dest{denial_code}  = "";
        $dest{denial_date}  = "";
	@fields=();				# release memory.
	
	print_record(\%dest);
	%dest=();
}
exit 0;




